home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / zextract.c < prev    next >
C/C++ Source or Header  |  1996-02-11  |  11KB  |  328 lines

  1. /* Handle a .class file embedded in a .zip archive.
  2.    This extracts a member from a .zip file, but does not handle
  3.    uncompression (since that is not needed for classes.zip).
  4.  
  5. Copyright (c) 1996 Cygnus Support
  6.  
  7. See the file "license.terms" for information on usage and redistribution
  8. of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  9.  
  10. Written by Per Bothner <bothner@cygnus.com>, February 1996.
  11. */
  12.  
  13. #include "zipfile.h"
  14.  
  15.  
  16. /* This stuff is partly based on the 28 August 1994 public release of the
  17. Info-ZIP group's portable UnZip zipfile-extraction program (and related
  18. utilities). */
  19.  
  20. #include <stdio.h>
  21. #ifdef __STDC__
  22. #include <stdlib.h>
  23. #endif
  24. #include <errno.h>       /* used in mapname() */
  25.  
  26. /*************/
  27. /*  Defines  */
  28. /*************/
  29.  
  30. #define UNZIP
  31. #define UNZIP_VERSION     20   /* compatible with PKUNZIP 2.0 */
  32. #define VMS_UNZIP_VERSION 42   /* if OS-needed-to-extract is VMS:  can do */
  33.  
  34.  
  35. #define ZSUFX             ".zip"
  36. #define CENTRAL_HDR_SIG   "\113\001\002"   /* the infamous "PK" signature */
  37. #define LOCAL_HDR_SIG     "\113\003\004"   /*  bytes, sans "P" (so unzip */
  38. #define END_CENTRAL_SIG   "\113\005\006"   /*  executable not mistaken for */
  39. #define EXTD_LOCAL_SIG    "\113\007\010"   /*  zipfile itself) */
  40.  
  41. #define STORED            0    /* compression methods */
  42. #define SHRUNK            1
  43. #define REDUCED1          2
  44. #define REDUCED2          3
  45. #define REDUCED3          4
  46. #define REDUCED4          5
  47. #define IMPLODED          6
  48. #define TOKENIZED         7
  49. #define DEFLATED          8
  50. #define NUM_METHODS       9    /* index of last method + 1 */
  51. /* don't forget to update list_files() appropriately if NUM_METHODS changes */
  52.  
  53. #define PK_OK             0    /* no error */
  54. #define PK_COOL           0    /* no error */
  55. #define PK_GNARLY         0    /* no error */
  56. #define PK_WARN           1    /* warning error */
  57. #define PK_ERR            2    /* error in zipfile */
  58. #define PK_BADERR         3    /* severe error in zipfile */
  59. #define PK_MEM            4    /* insufficient memory */
  60. #define PK_MEM2           5    /* insufficient memory */
  61. #define PK_MEM3           6    /* insufficient memory */
  62. #define PK_MEM4           7    /* insufficient memory */
  63. #define PK_MEM5           8    /* insufficient memory */
  64. #define PK_NOZIP          9    /* zipfile not found */
  65. #define PK_PARAM          10   /* bad or illegal parameters specified */
  66. #define PK_FIND           11   /* no files found */
  67. #define PK_DISK           50   /* disk full */
  68. #define PK_EOF            51   /* unexpected EOF */
  69.  
  70. /*---------------------------------------------------------------------------
  71.     True sizes of the various headers, as defined by PKWARE--so it is not
  72.     likely that these will ever change.  But if they do, make sure both these
  73.     defines AND the typedefs below get updated accordingly.
  74.   ---------------------------------------------------------------------------*/
  75. #define LREC_SIZE     26    /* lengths of local file headers, central */
  76. #define CREC_SIZE     42    /*  directory headers, and the end-of-    */
  77. #define ECREC_SIZE    18    /*  central-dir record, respectively      */
  78.  
  79.  
  80. #ifndef SEEK_SET
  81. #  define SEEK_SET  0
  82. #  define SEEK_CUR  1
  83. #  define SEEK_END  2
  84. #endif
  85.  
  86. /**************/
  87. /*  Typedefs  */
  88. /**************/
  89.  
  90. typedef char              boolean;
  91. typedef unsigned char     uch;  /* code assumes unsigned bytes; these type-  */
  92. typedef unsigned short    ush;  /*  defs replace byte/UWORD/ULONG (which are */
  93. typedef unsigned long     ulg;  /*  predefined on some systems) & match zip  */
  94.  
  95. /*---------------------------------------------------------------------------
  96.     Zipfile layout declarations.  If these headers ever change, make sure the
  97.     xxREC_SIZE defines (above) change with them!
  98.   ---------------------------------------------------------------------------*/
  99.  
  100.    typedef uch   local_byte_hdr[ LREC_SIZE ];
  101. #      define L_VERSION_NEEDED_TO_EXTRACT_0     0
  102. #      define L_VERSION_NEEDED_TO_EXTRACT_1     1
  103. #      define L_GENERAL_PURPOSE_BIT_FLAG        2
  104. #      define L_COMPRESSION_METHOD              4
  105. #      define L_LAST_MOD_FILE_TIME              6
  106. #      define L_LAST_MOD_FILE_DATE              8
  107. #      define L_CRC32                           10
  108. #      define L_COMPRESSED_SIZE                 14
  109. #      define L_UNCOMPRESSED_SIZE               18
  110. #      define L_FILENAME_LENGTH                 22
  111. #      define L_EXTRA_FIELD_LENGTH              24
  112.  
  113.   typedef uch   cdir_byte_hdr[ CREC_SIZE ];
  114. #      define C_VERSION_MADE_BY_0               0
  115. #      define C_VERSION_MADE_BY_1               1
  116. #      define C_VERSION_NEEDED_TO_EXTRACT_0     2
  117. #      define C_VERSION_NEEDED_TO_EXTRACT_1     3
  118. #      define C_GENERAL_PURPOSE_BIT_FLAG        4
  119. #      define C_COMPRESSION_METHOD              6
  120. #      define C_LAST_MOD_FILE_TIME              8
  121. #      define C_LAST_MOD_FILE_DATE              10
  122. #      define C_CRC32                           12
  123. #      define C_COMPRESSED_SIZE                 16
  124. #      define C_UNCOMPRESSED_SIZE               20
  125. #      define C_FILENAME_LENGTH                 24
  126. #      define C_EXTRA_FIELD_LENGTH              26
  127. #      define C_FILE_COMMENT_LENGTH             28
  128. #      define C_DISK_NUMBER_START               30
  129. #      define C_INTERNAL_FILE_ATTRIBUTES        32
  130. #      define C_EXTERNAL_FILE_ATTRIBUTES        34
  131. #      define C_RELATIVE_OFFSET_LOCAL_HEADER    38
  132.  
  133.    typedef uch   ec_byte_rec[ ECREC_SIZE+4 ];
  134. /*     define SIGNATURE                         0   space-holder only */
  135. #      define NUMBER_THIS_DISK                  4
  136. #      define NUM_DISK_WITH_START_CENTRAL_DIR   6
  137. #      define NUM_ENTRIES_CENTRL_DIR_THS_DISK   8
  138. #      define TOTAL_ENTRIES_CENTRAL_DIR         10
  139. #      define SIZE_CENTRAL_DIRECTORY            12
  140. #      define OFFSET_START_CENTRAL_DIRECTORY    16
  141. #      define ZIPFILE_COMMENT_LENGTH            20
  142.  
  143.  
  144.    typedef struct local_file_header {                 /* LOCAL */
  145.        uch version_needed_to_extract[2];
  146.        ush general_purpose_bit_flag;
  147.        ush compression_method;
  148.        ush last_mod_file_time;
  149.        ush last_mod_file_date;
  150.        ulg crc32;
  151.        ulg csize;
  152.        ulg ucsize;
  153.        ush filename_length;
  154.        ush extra_field_length;
  155.    } local_file_hdr;
  156.  
  157.    typedef struct central_directory_file_header {     /* CENTRAL */
  158.        uch version_made_by[2];
  159.        uch version_needed_to_extract[2];
  160.        ush general_purpose_bit_flag;
  161.        ush compression_method;
  162.        ush last_mod_file_time;
  163.        ush last_mod_file_date;
  164.        ulg crc32;
  165.        ulg csize;
  166.        ulg ucsize;
  167.        ush filename_length;
  168.        ush extra_field_length;
  169.        ush file_comment_length;
  170.        ush disk_number_start;
  171.        ush internal_file_attributes;
  172.        ulg external_file_attributes;
  173.        ulg relative_offset_local_header;
  174.    } cdir_file_hdr;
  175.  
  176.    typedef struct end_central_dir_record {            /* END CENTRAL */
  177.        ush number_this_disk;
  178.        ush num_disk_with_start_central_dir;
  179.        ush num_entries_centrl_dir_ths_disk;
  180.        ush total_entries_central_dir;
  181.        ulg size_central_directory;
  182.        ulg offset_start_central_directory;
  183.        ush zipfile_comment_length;
  184.    } ecdir_rec;
  185.  
  186.  
  187. /************/
  188. /*  Macros  */
  189. /************/
  190.  
  191. #ifndef MAX
  192. #  define MAX(a,b)   ((a) > (b) ? (a) : (b))
  193. #endif
  194. #ifndef MIN
  195. #  define MIN(a,b)   ((a) < (b) ? (a) : (b))
  196. #endif
  197.  
  198.  
  199. /***********************/
  200. /* Function makeword() */
  201. /***********************/
  202.  
  203. static ush makeword(b)
  204.     uch *b;
  205. {
  206.     /*
  207.      * Convert Intel style 'short' integer to non-Intel non-16-bit
  208.      * host format.  This routine also takes care of byte-ordering.
  209.      */
  210.     return (ush)((b[1] << 8) | b[0]);
  211. }
  212.  
  213.  
  214. /***********************/
  215. /* Function makelong() */
  216. /***********************/
  217.  
  218. static ulg makelong(sig)
  219.     uch *sig;
  220. {
  221.     /*
  222.      * Convert intel style 'long' variable to non-Intel non-16-bit
  223.      * host format.  This routine also takes care of byte-ordering.
  224.      */
  225.     return (((ulg)sig[3]) << 24)
  226.         + (((ulg)sig[2]) << 16)
  227.         + (((ulg)sig[1]) << 8)
  228.         + ((ulg)sig[0]);
  229. }
  230.  
  231. int
  232. read_zip_archive (zipf)
  233.      register ZipFile *zipf;
  234. {
  235.   int i;
  236.   int dir_last_pad;
  237.   char *dir_ptr;
  238.   char buffer[100];
  239.   zipf->size = lseek (zipf->fd, 0L, SEEK_END);
  240.   if (zipf->size < (ECREC_SIZE+4) || lseek (zipf->fd, -(ECREC_SIZE+4), SEEK_CUR) <= 0)
  241.     return -1;
  242.   if (read (zipf->fd, buffer, ECREC_SIZE+4) != ECREC_SIZE+4)
  243.     return -2;
  244.   zipf->count = makeword(&buffer[TOTAL_ENTRIES_CENTRAL_DIR]);
  245.   zipf->dir_size = makelong(&buffer[SIZE_CENTRAL_DIRECTORY]);
  246. #define ALLOC malloc
  247.   /* Allocate 1 more to allow appending '\0' to last filename. */
  248.   zipf->central_directory = ALLOC (zipf->dir_size+1);
  249.   if (lseek (zipf->fd, -(zipf->dir_size+ECREC_SIZE+4), SEEK_CUR) < 0)
  250.     return -2;
  251.   if (read (zipf->fd, zipf->central_directory, zipf->dir_size) < 0)
  252.     return -2;
  253.  
  254. #ifdef TEST
  255.   printf ("number_this_disk = %d\n", makeword(&buffer[NUMBER_THIS_DISK]));
  256.   printf ("num_disk_with_start_central_dir = %d\n", makeword(&buffer[NUM_DISK_WITH_START_CENTRAL_DIR]));
  257.  
  258.   printf ("num_entries_centrl_dir_ths_disk = %d\n",
  259.         makeword(&buffer[NUM_ENTRIES_CENTRL_DIR_THS_DISK]));
  260.   printf ("total_entries_central_dir = %d\n",
  261.         makeword(&buffer[TOTAL_ENTRIES_CENTRAL_DIR]));
  262.   printf ("size_central_directory = %d\n",
  263.         makelong(&buffer[SIZE_CENTRAL_DIRECTORY]));
  264.   printf ("offset_start_central_directory = %d\n",
  265.         makelong(&buffer[OFFSET_START_CENTRAL_DIRECTORY]));
  266.   printf ("zipfile_comment_length = %d\n",
  267.         makeword(&buffer[ZIPFILE_COMMENT_LENGTH]));
  268. #endif
  269.  
  270.   dir_last_pad = 0;
  271.   dir_ptr = zipf->central_directory;
  272.   for (i = 0; i < zipf->count; i++)
  273.     {
  274.       ZipDirectory *zipd = (ZipDirectory*)(dir_ptr + dir_last_pad);
  275.       long uncompressed_size = makelong (&dir_ptr[4+C_UNCOMPRESSED_SIZE]);
  276.       long filename_length = makeword (&dir_ptr[4+C_FILENAME_LENGTH]);
  277.       int unpadded_direntry_length;
  278.       if ((dir_ptr-zipf->central_directory)+filename_length+CREC_SIZE+4>zipf->dir_size)
  279.     return -1;
  280.  
  281.       zipd->filename_length = filename_length;
  282.       zipd->size = uncompressed_size;
  283. #ifdef __GNUC__
  284. #define DIR_ALIGN __alignof__(ZipDirectory)
  285. #else
  286. #define DIR_ALIGN sizeof(long)
  287. #endif
  288.       zipd->filestart = makelong (&dir_ptr[4+C_RELATIVE_OFFSET_LOCAL_HEADER])
  289.     + (LREC_SIZE+4) + filename_length;
  290.       zipd->filename_offset = CREC_SIZE+4 - dir_last_pad;
  291.       unpadded_direntry_length = zipd->filename_offset + zipd->filename_length;
  292.       zipd->direntry_size =
  293.     ((unpadded_direntry_length + DIR_ALIGN) / DIR_ALIGN) * DIR_ALIGN;
  294.       dir_last_pad = zipd->direntry_size - unpadded_direntry_length;
  295.  
  296.       dir_ptr = (char*)zipd + unpadded_direntry_length;
  297.       *dir_ptr = '\0';
  298.     }
  299.   return 0;
  300. }
  301.  
  302. #ifdef TEST
  303. main ()
  304. {
  305.   ZipFile zipf[1];
  306.   ZipDirectory *zipd;
  307.   int i;
  308.  
  309.   zipf->fd = 0;
  310.  
  311.   i = read_zip_archive (zipf);
  312.   if (i)
  313.     {
  314.       fprintf (stderr, "Bad zip file.\n");
  315.       exit (i);
  316.     }
  317.  
  318.   zipd = (ZipDirectory*) zipf->central_directory;
  319.   for (i = 0; i < zipf->count; i++, zipd = ZIPDIR_NEXT (zipd))
  320.     {
  321.       printf ("%d: size:%d, name(#%d)%s, offset:%d\n",
  322.           i, zipd->size, zipd->filename_length,
  323.           ZIPDIR_FILENAME (zipd),
  324.           zipd->filestart);
  325.     }
  326. }
  327. #endif
  328.